/* 
	Copyright (c) Whitemice Consulting, 2007, 2008, 2009
  Adam Tauno Williams <awilliam@whitemice.org>
  License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;

namespace Whitemice.ZOGI.RPC2
{
  /// <summary>
  /// ZOGIProtocolException objects should only be thrown by the RpcWasher
  /// </summary>
  public class ZOGIProtocolException : Exception
  {   
    public ZOGIProtocolException(string _message) : base(_message)
    {
    }
    
    public ZOGIProtocolException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
  
  /// <summary>
  /// NonIntegerObjectIdException objects are thrown when the objectId of an
  /// entity received from the server is not an integer.
  /// </summary>
  public class NonIntegerObjectIdException : ZOGIProtocolException
  {
    public NonIntegerObjectIdException(string _message) : base(_message)
    {
    }
    
    public NonIntegerObjectIdException(object _objectId)
      : base(string.Format("objectId {0} of type {1}",
               _objectId.ToString(),
               _objectId.GetType().ToString()))
    {
    }
    
    public NonIntegerObjectIdException(object _objectId, Exception _exception)
      : base(string.Format("objectId {0} of type {1}",
               _objectId.ToString(),
               _objectId.GetType().ToString()),
             _exception)
    {
    }    
  }  

  /// <summary>
  /// NonIntegerVersionException are thrown when the version attribute of an entity
  /// recieved from the server is not an integer.
  /// </summary>
  public class NonIntegerVersionException : ZOGIProtocolException
  {
    public NonIntegerVersionException(string _message) : base(_message)
    {
    }
    
    public NonIntegerVersionException(System.Type _type)
      : base(string.Format("version has type {0}", _type.ToString()))
    {
    }
    
    public NonIntegerVersionException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
  
  /// <summary>
  /// EntityWithNoEntityNameException objects are thrown when an entity is
  /// received from the server that does not have an entityName attribute.
  /// The zOGI protocol requires that all entities have an entityName.
  /// </summary>
  public class EntityWithNoEntityNameException : ZOGIProtocolException
  {
    public EntityWithNoEntityNameException() 
      : this(null)
    {
    }
    
    public EntityWithNoEntityNameException(Exception _exception)
      : base("Entity with no entityName encountered.", _exception)
    {
    }
  }
  
  /// <summary>
  /// NonArraySubordinateKeyException objects are thrown when a subordinate key
  /// of an entity is processed whose value is not an array.  Any key whose
  /// name starts with an underscore is considered a subordinate key and the
  /// content of the attribute must be an array;  the array may be empty.
  /// </summary>
  public class NonArraySubordinateKeyException : ZOGIProtocolException
  {
    public NonArraySubordinateKeyException(string _key) 
      : base(string.Format("Non-array for subordinate key {0}", _key))
    {
    }

    public NonArraySubordinateKeyException(string _key, System.Type _type)
      : base(string.Format("key {0} value has type {1}", _key, _type.ToString()))
    {
    }
    
    public NonArraySubordinateKeyException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
  
  /// <summary>
  /// NonStringKeyException object is thrown when an entity contains an attribute
  /// whose value is exected to be a string but it is not.
  /// </summary>
  public class NonStringKeyException : ZOGIProtocolException
  {
    public NonStringKeyException(object _key) : this(_key.GetType())
    {
    }

    public NonStringKeyException(System.Type _type)
      : base(string.Format("key of struct has type {0}", _type.ToString()))
    {
    }
    
    public NonStringKeyException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
  
  public class NonStructInResultsException : ZOGIProtocolException
  {
    public NonStructInResultsException(string _message) : base(_message)
    {
    }

    public NonStructInResultsException(System.Type _type)
      : base(string.Format("object in results has type {0}", _type.ToString()))
    {
    }
    
    public NonStructInResultsException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
  
  public class EntityDecodeException : ZOGIProtocolException
  {
    public EntityDecodeException(string _message) : base(_message)
    {
    }
    
    public EntityDecodeException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }

  public class NullValueForKeyException : EntityDecodeException
  {
    public NullValueForKeyException(string _key) 
      : this(_key, null)
    {
    }
    
    public NullValueForKeyException(string _key, Exception _exception)
      : base(string.Format("NULL value for struct key {0}", _key), 
             _exception)
    {
    }
  }
  
  /// <summary>
  /// A MissingMandatoryKeyException object is thrown when an entity lacks a 
  /// required key.
  /// </summary>
  public class MissingMandatoryKeyException : ZOGIProtocolException
  {
    public MissingMandatoryKeyException(
      IDictionary<string, object> _hash, 
      string _key)
      : base(string.Format("Missing mandatory key {0} in {1}#{2}",
               _key,
               _hash["entityName"],
               _hash["objectId"]),
             null)
    {
    }
  
    public MissingMandatoryKeyException(
      IDictionary<string, object> _hash, 
      string _key,
      Exception _inner)
      : base(string.Format("Missing mandatory key {0} in {1}#{2}",
               _key,
               _hash["entityName"],
               _hash["objectId"]),
             _inner)
    {
    }
  
    public MissingMandatoryKeyException(string _message) : base(_message)
    {
    }
    
    public MissingMandatoryKeyException(string _message, Exception _exception)
      : base(_message, _exception)
    {
    }
  }
}